package com.bjfu.demo.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.bjfu.demo.dao.DataMapper;
import com.bjfu.demo.entity.Data;
import com.bjfu.demo.entity.DataExample;
import com.bjfu.demo.entity.SiteData;
import com.bjfu.demo.entity.pojo.RpCondition;
import com.bjfu.demo.entity.pojo.RpResult;
import com.bjfu.demo.utils.AvgUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @create 2021-02-22 10:50
 */
@Service
@Slf4j
public class DataService {

    @Autowired
    private DataMapper dataMapper;

    @Autowired
    private SiteDataService siteDataService;

    //test
    public Object get(Integer id) {

        DataExample dataExample = new DataExample();
        DataExample.Criteria criteria = dataExample.createCriteria();
        criteria.andIdEqualTo(id);
        List<Data> data = dataMapper.selectByExample(dataExample);
        return data;
    }

    public List<RpResult> getRpData(RpCondition condition) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(condition.getDate());
        calendar.add(Calendar.DATE, 1);
        log.info("开始按照{}查询,截止日期{}", condition, calendar.getTime());
        List<SiteData> siteData = siteDataService.getSiteId(condition.getDistrict());
        List<String> ids = siteData.stream().map(SiteData::getSiteid).collect(Collectors.toList());
        ids.forEach(System.out::println);
        DataExample dataExample = new DataExample();
        DataExample.Criteria criteria = dataExample.createCriteria();
        criteria.andSiteidIn(ids);
        criteria.andCollecttimeBetween(condition.getDate(), calendar.getTime());
        List<Data> data = dataMapper.selectByExample(dataExample);
        Map<String, List<Data>> dataMap = data.stream().collect(Collectors.groupingBy(Data::getSiteid));
        List<RpResult> result = new ArrayList<>();
        for (SiteData siteDatum : siteData) {
            RpResult rpResult = new RpResult();
            rpResult.setCity(condition.getDistrict());
            List<Data> dataList = dataMap.get(siteDatum.getSiteid());
            if (CollectionUtils.isEmpty(dataList)) {
                dataList = new ArrayList<>();
            }
            int levelCount = dataList.size();
            long level1 = dataList.stream().filter(d -> d.getAirlevelcode().equals("ⅰ")).count();
            long level2 = dataList.stream().filter(d -> d.getAirlevelcode().equals("ⅱ")).count();
            long level3 = dataList.stream().filter(d -> d.getAirlevelcode().equals("ⅲ")).count();
            long level4 = dataList.stream().filter(d -> d.getAirlevelcode().equals("ⅳ")).count();

            List<Long> levelList = new ArrayList<>();
            levelList.add(level1);
            levelList.add(level2);
            levelList.add(level3);
            levelList.add(level4);
            //有数据
            if (levelCount != 0) {
                rpResult.setSITENAME(siteDatum.getSitename());
                rpResult.setSITEID(siteDatum.getSiteid());
                if ("ⅰ".equals(condition.getLevel()) && level1 != 0) {
                    if (!abiggerOrEqThanOthers(level1, levelList)) {
                        continue;
                    }
                    rpResult.setAIRLEVELNAME("很清新");
                } else if ("ⅱ".equals(condition.getLevel())&& level2 != 0) {
                    if (!abiggerOrEqThanOthers(level2, levelList)) {
                        continue;
                    }
                    rpResult.setAIRLEVELNAME("清新");
                } else if ("ⅲ".equals(condition.getLevel())&& level3 != 0) {
                    if (!abiggerOrEqThanOthers(level3, levelList)) {
                        continue;
                    }
                    rpResult.setAIRLEVELNAME("一般");
                } else if ("ⅳ".equals(condition.getLevel())&& level4 != 0) {
                    if (!abiggerOrEqThanOthers(level4, levelList)) {
                        continue;
                    }
                    rpResult.setAIRLEVELNAME("不清新");
                } else {
                    continue;
                }
                rpResult.setSearchTime(condition.getDate());

                List<String> col = data.stream()
                        .filter(d -> d.getSiteid().equals(siteDatum.getSiteid()) && !"--".equals(d.getAirlevelcode()))
                        .map(Data::getCol1).collect(Collectors.toList());
                rpResult.setCOL1(AvgUtils.getAvgFromString(col));

                col = data.stream()
                        .filter(d -> d.getSiteid().equals(siteDatum.getSiteid()) && !"--".equals(d.getAirlevelcode()))
                        .map(Data::getCol2).collect(Collectors.toList());
                rpResult.setCOL2(AvgUtils.getAvgFromString(col));

                col = data.stream()
                        .filter(d -> d.getSiteid().equals(siteDatum.getSiteid()) && !"--".equals(d.getAirlevelcode()))
                        .map(Data::getCol3).collect(Collectors.toList());

                rpResult.setCOL3(AvgUtils.getAvgFromString(col));
                result.add(rpResult);
            }
        }
        return result;
    }


    private boolean abiggerOrEqThanOthers(long level1, List<Long> others) {

        for (Long other : others) {
            if (level1 < other) {
                return false;
            }
        }

        return true;

    }
}
